/***************************************************************************
*   Copyright (C) 2011 by Chuck Gao & Alex Liu                             *
*                                                                          *
*   This program is free software: you can redistribute it and/or modify   *
*   it under the terms of the GNU General Public License as published by   *
*   the Free Software Foundation, either version 3 of the License, or      *
*   (at your option) any later version.                                    *
*                                                                          *
*   This program is distributed in the hope that it will be useful,        *
*   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
*   GNU General Public License for more details.                           *
*                                                                          *
*   You should have received a copy of the GNU General Public License      *
*   along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
*                                                                          *
*   Contact author: chuckgao.cg@gmail.com                                  *
*                   alex2202375@gmail.com                                  *
***************************************************************************/

import QtQuick 1.0
import com.nokia.symbian 1.1
import Qt.labs.components 1.1
import Qt.labs.shaders 1.0

Item {
    id: mealPlanMainWindow
    width: 360
    height: 640
    clip: true

    Item {
        id: planView
        width: parent.width
        height: parent.height - titleBar.height
        y: titleBar.height

        onYChanged: {
            if( planView.y <= -60 )
            {
                indicatorBar.visible = true
            }else{
                indicatorBar.visible = false
            }
        }

        Image {
            // Bg
            id: bg
            anchors.fill: parent
            opacity: 0.8
            source: "qrc:/plan/Images/Plan/bg.jpg"
        }

        ShaderEffectSource {
            id: thesource
            sourceItem: bg
            live: false
            hideSource: true
        }

        RadialWaveEffect {
            id: layer
            anchors.fill: parent;
            source: thesource

            wave: 0.0
            waveOriginX: 0.5
            waveOriginY: 0.5
            waveWidth: 0.01

            NumberAnimation on wave {
                id: waveAnim
                running: true
                loops: Animation.Infinite
                easing.type: "Linear"
                from: 0.0000; to: 2.0000;
                duration: 2500
            }
        }

        MealPlanDemonsView {
            id: demonsArea
            width: parent.width
            height: parent.height - toolBar.height
        }

        Item {
            id: toolBar
            width: parent.width
            height: 45
            anchors.left: parent.left
            anchors.top: demonsArea.bottom
            anchors.bottom: parent.bottom

            Image {
                width: 40
                height: 40
                anchors.left: parent.left
                anchors.leftMargin: 10
                anchors.verticalCenter: parent.verticalCenter
                smooth: true
                source: "qrc:/plan/Images/Plan/previous.png"
            }

            Component {
                id: singleSelectionDialogComponent
                SelectionDialog {
                    titleText: "Select Plan Category"
                    selectedIndex: 1

                    model: ListModel {
                        id: colorModel

                        ListElement { name: "Morning" }
                        ListElement { name: "Noon" }
                        ListElement { name: "Dinner" }
                        ListElement { name: "Midnight snack" }
                        ListElement { name: "Shopping" }
                    }

                    onAccepted: {
                        selectionDialogButton.text = colorModel.get(selectedIndex).name
                        selectionDialogButton.checked = false
                    }

                    onRejected: {
                       selectionDialogButton.checked = false
                    }
                }
            }

            Button {
                id: selectionDialogButton
                anchors.centerIn: parent
                text: "Morning"
            }

            Image {
                width: 40
                height: 40
                anchors.right: parent.right
                anchors.rightMargin: 10
                anchors.verticalCenter: parent.verticalCenter
                smooth: true
                source: "qrc:/plan/Images/Plan/next.png"
            }

            property SelectionDialog singleSelectionDialog
            MouseArea {
                anchors.fill: toolBar
                drag.target: planView;
                drag.axis: Drag.YAxis
                drag.minimumX: 0;
                drag.maximumX: 100

                onClicked: {
                    if ( mouse.x < 120 || mouse.x > 240 )
                        return;

                    if (!toolBar.singleSelectionDialog)
                        toolBar.singleSelectionDialog = singleSelectionDialogComponent.createObject(mealPlanMainWindow)

                    selectionDialogButton.checked = true
                    toolBar.singleSelectionDialog.open()
                }

                onReleased: {
                    // If the we need show the extenal menu area
                    if( planView.y < -60 )
                    {
                        comebackAnimation.to = -60
                        comebackAnimation.running = true
                    }else{
                        comebackAnimation.to = titleBar.height
                        comebackAnimation.running = true
                    }
                }
            }
        }

        NumberAnimation {
            id: comebackAnimation
            target: planView;
            property: "y";
            duration: 600
            easing.type: Easing.OutExpo
            running: false
        }

        Rectangle {
            id: indicatorBar
            width: parent.width
            height: 3
            anchors.left: parent.left
            anchors.bottom: parent.bottom
            anchors.bottomMargin: -indicatorBar.height
            visible: false
            color: "orange"
            opacity: 0.6
        }
    }


    Rectangle {
        id: titleBar
        width: parent.width
        height: 60
        color: "gray"
    }

}
